package com.qdlc.p2p.biz.executer.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.qdlc.p2p.biz.enums.EnumRuleType;
import com.qdlc.p2p.biz.enums.EnumTache;
import com.qdlc.p2p.biz.service.BaseGiveAwardService;
import com.qdlc.p2p.biz.service.NewActivityPlanService;
import com.qdlc.p2p.biz.service.NewActivityRuleService;
import com.qdlc.p2p.biz.util.ActivityTools;
import com.qdlc.p2p.common.util.BeanUtil;
import com.qdlc.p2p.dal.dto.NewActivityPlan;
import com.qdlc.p2p.dal.dto.NewActivityRule;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.model.BaseGiveAwardModel;

/**
 * Created by liaoxiangkai on 2015/12/28.
 */
public class NewActivityAwardExecutor {

    //赠送服务
    protected static BaseGiveAwardService giveUserRedEnvelopeService;

    protected static BaseGiveAwardService giveUserExperienceService;

    protected static BaseGiveAwardService giveUserUpRateService;

    protected static BaseGiveAwardService giveUserLotteryService;

    protected static BaseGiveAwardService giveUserMovieTicketService;

    protected static BaseGiveAwardService giveUserCommodityService;

    protected static Map<Integer, BaseGiveAwardService> map;

    //活动规则服务
    protected static NewActivityPlanService newActivityPlanService = (NewActivityPlanService) BeanUtil.getBean("newActivityPlanService");
    protected static NewActivityRuleService newActivityRuleService = (NewActivityRuleService) BeanUtil.getBean("newActivityRuleService");

    static {
        giveUserRedEnvelopeService = (BaseGiveAwardService) BeanUtil.getBean("giveUserRedEnvelopeService");
        giveUserUpRateService = (BaseGiveAwardService) BeanUtil.getBean("giveUserUpRateService");
        giveUserExperienceService = (BaseGiveAwardService) BeanUtil.getBean("giveUserExperienceService");
        giveUserLotteryService = (BaseGiveAwardService) BeanUtil.getBean("giveUserLotteryService");
        giveUserMovieTicketService = (BaseGiveAwardService) BeanUtil.getBean("giveUserMovieTicketService");
        giveUserCommodityService = (BaseGiveAwardService) BeanUtil.getBean("giveUserCommodityService");

        map = new HashMap<Integer, BaseGiveAwardService>();
        map.put(EnumRuleType.TYPE_UP_RATE.getType(), giveUserUpRateService);
        map.put(EnumRuleType.TYPE_LOTTERY.getType(), giveUserLotteryService);
        map.put(EnumRuleType.TYPE_MOVIE_TICKETS.getType(), giveUserMovieTicketService);
        map.put(EnumRuleType.TYPE_RED_ENVELOPE.getType(), giveUserRedEnvelopeService);
        map.put(EnumRuleType.TYPE_VIRTUAL_RED_ENVELOPE.getType(), giveUserRedEnvelopeService);
        map.put(EnumRuleType.TYPE_EXPERIENCE_COUPON.getType(), giveUserExperienceService);
    }


    /**
     * 抽奖赠送奖品
     *
     * @param type
     * @param baseGiveAwardModel
     */
    public static void give(int type, BaseGiveAwardModel baseGiveAwardModel) {
        BaseGiveAwardService baseGiveAwardService = map.get(type);
        if (null != baseGiveAwardModel)
            baseGiveAwardService.giveSingleUserAward(baseGiveAwardModel);
    }

    /**
     * 通过活动环节发放
     *
     * @param user  用户
     * @param tache 环节
     */
    public static void giveByTache(User user, String tache) {
        giveByTache(user, tache, null);
    }

    /**
     * 通过活动环节发放
     *
     * @param user    用户
     * @param tache   环节
     * @param extMpas 额外参数
     */
    public static void giveByTache(User user, String tache, Map<String, Object> extMpas) {

        NewActivityPlan activityPlan = newActivityPlanService.getActivityPlanByTache(tache);
        // 校验本活动是否可用
        boolean isAvailable = ActivityTools.checkActivity(activityPlan, user.getUserId());
        if (!isAvailable) return;
        //查询活动规则表，如果为空直接返回
        List<NewActivityRule> list = newActivityRuleService.findByActivityId(activityPlan.getId());
        if (list.isEmpty() || list.size() <= 0) return;
        for (NewActivityRule entity : list) {
            try {
                // 校验本条规则是否可用
                if (!ActivityTools.checkRule(entity, user.getUserId())) continue;
                BaseGiveAwardService baseGiveAwardService = (BaseGiveAwardService) BeanUtil.getBean(entity.getBusinessClass());
                BaseGiveAwardModel baseGiveAwardModel = new BaseGiveAwardModel();
                baseGiveAwardModel.setUser(user);
                baseGiveAwardModel.setRuleId(entity.getRuleId());
                baseGiveAwardModel.setSourceType(tache);
                baseGiveAwardModel.setRuleType(entity.getType());
                baseGiveAwardModel.setDescription(EnumTache.getTacheByEnName(tache));
                baseGiveAwardModel.setExtMpas(extMpas);
                baseGiveAwardService.giveAward(baseGiveAwardModel);
            } catch (Exception ex) {
                // 活动出错 继续下一个活动 避免出错而中断
                ex.printStackTrace();
            }
        }
    }


}
